package com.loooooo.pm2024.modules.pm.service;


import com.loooooo.pm2024.exception.MyBaseException;
import com.loooooo.pm2024.modules.pm.mapper.IssueCommentMapper;
import com.loooooo.pm2024.modules.pm.mapper.IssueMapper;
import com.loooooo.pm2024.modules.pm.model.Attachment;
import com.loooooo.pm2024.modules.pm.model.Issue;
import com.loooooo.pm2024.modules.pm.model.IssueComment;
import com.loooooo.pm2024.modules.pm.model.Task;
import com.loooooo.pm2024.modules.pm.selector.AttachmentSelector;
import com.loooooo.pm2024.modules.pm.selector.IssueCommentSelector;
import com.loooooo.pm2024.modules.pm.selector.IssueSelector;
import com.loooooo.pm2024.modules.pm.selector.TaskSelector;
import com.loooooo.pm2024.security.SecurityUtils;
import com.loooooo.pm2024.service.IdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class IssueCommentService {

    @Autowired
    IssueCommentMapper commentMapper;
    @Autowired
    IdService idService;
    @Autowired
    TaskService taskService;
    @Autowired
    AttachmentService attachmentService;

    @Autowired
    IssueService issueService;
    @Autowired
    TransactionTemplate transactionTemplate;

    public IssueComment get(Long id) {
        return get(id, null);
    }


    public IssueComment get(Long id, IssueCommentSelector selector) {
        IssueComment comment = commentMapper.get(id);
        if (selector != null && comment != null) {

        }
        return comment;
    }

    public List<IssueComment> list(IssueCommentSelector selector) {
        List<IssueComment> list = commentMapper.select(selector);
        if (list.size() > 0) {
            if (Objects.equals(1, selector.getWithAttachmentList())) {
                AttachmentSelector attachmentSelector = new AttachmentSelector();
                attachmentSelector.setCommentIdList(list.stream().map(x -> x.getId()).distinct().collect(Collectors.toList()));
                List<Attachment> attachmentList = attachmentService.list(attachmentSelector);
                if (attachmentList.size() > 0) {
                    for (IssueComment issue : list) {
                        issue.setAttachmentList(attachmentList.stream().filter(x -> Objects.equals(issue.getId(), x.getCommentId())).collect(Collectors.toList()));
                    }
                }
            }
        }
        return list;
    }

    public int count(IssueCommentSelector selector) {
        return commentMapper.count(selector);
    }

    public int update(IssueComment issueComment) {
        long now = System.currentTimeMillis();
        issueComment.setUpdatedAt(now);
        Long userId = SecurityUtils.loginedUserId();
        issueComment.setUpdatedBy(userId);

        if (issueComment.getId() == null || issueComment.isNewEntity() || issueComment.getId() <= 0L) {
            if (userId == null) {
                MyBaseException.throwMe(10401, "未登录");
            }
            if (issueComment.getId() == null || issueComment.getId() <= 0L) {
                issueComment.setId(idService.nextId());
            }
            if (issueComment.getBusinessId() == null) {
                MyBaseException.throwMe(10500, "业务ID不能为空");
            }
            if (issueComment.getType() == null) {
                MyBaseException.throwMe(10500, "类型不能为空");
            }

            if (issueComment.getIssueId() == null) {
                //MyBaseException.throwMe(10500, "所属主题不能为空");
            }
            if (issueComment.getBusinessId() != null && Objects.equals(issueComment.getBusinessId(), issueComment.getIssueId())) {
                Issue issue = issueService.get(issueComment.getIssueId());
                issueComment.setNotifyTo(issue.getCreatedBy());
            }
            issueComment.setCreatedAt(now);
            issueComment.setCreatedBy(userId);
            commentMapper.insert(issueComment);
        }
        return commentMapper.update(issueComment);
    }

    public int like(Long commentId) {
        return 0;
    }

}
